package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zonglzp.entity.RepairEntity;
import org.jeecg.modules.zonglzp.service.CompanyService;
import org.jeecg.modules.zonglzp.service.impl.RepairServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/repair")
public class RepairController {


    @Autowired
    private CompanyService companyservice;

    @Autowired
    RepairServiceImpl service;

    /*
    根据快递id获取快递名称
     */
    @GetMapping("/getcourierNamebyId")
    public JSONObject getcourierNamebyId(
            @RequestParam(name = "id") int id
    ){
        JSONObject obj = new JSONObject();
       String courierName=service.getcourierNamebyId(id);
        obj.put("data", courierName);
        return obj;
    }


    /*
     新增返修
     */
    @PostMapping("/addrepair")
    public JSONObject addrepair(RepairEntity entity) {

        JSONObject obj = new JSONObject();
        //这里把新增返修的信息插入到返修表
        int count = service.addrepair(entity);


        if (count > 0) {
            obj.put("code", 0);
            obj.put("message", "新增成功");
        } else {
            obj.put("code", 1);
            obj.put("message", "新增失败");
        }
        return obj;
    }


    /*
    返修查询
     */
    @GetMapping("/getrepairlist")
    public JSONObject getrepairlist(
            @RequestParam(name = "token") String token,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "type", defaultValue = "1") Integer type,
            String today,
            @RequestParam(name = "status", defaultValue = "11") Integer status,
            String orderId
    ) {
        JSONObject obj = new JSONObject();

        //通过用户token获取用户id
        String userid = companyservice.getUseridbytoken(token);
        if (userid == null) {
            obj.put("code", 1);
            obj.put("data", "token失效或存户不存在");
            return obj;
        }

        Page<RepairEntity> page = new Page<RepairEntity>(pageNo, pageSize);
        IPage<RepairEntity> pageList = service.getrepairlist(page, userid, today, orderId, type, status);

        if (pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
        } else {
            obj.put("code", 0);
            obj.put("data", "未查到数据");
        }
        return obj;
    }
}
